package com.wephone.Student.ui;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.Time;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.wephone.Student.R;
import com.wephone.Student.bean.data.CityData;
import com.wephone.Student.bean.data.CountryData;
import com.wephone.Student.bean.data.ProvinceData;
import com.wephone.Student.wheel.widget.OnWheelChangedListener;
import com.wephone.Student.wheel.widget.StringWheelAdapter;
import com.wephone.Student.wheel.widget.WheelView;

public class FlyTogetherSearchActivity extends BaseActivity {
	private int _year;
	private int _month;
	private int _day;
	private Time nowTime;
	private TextView flyDateText;
	private TextView flyFromEdit;
	private TextView flyToEdit;
	private EditText flySearchEdit;
	private int nCountryIdx;
	private int nProvinceIdx;
	private int nCityIdx;
	private int nCountryIdx2;
	private int nProvinceIdx2;
	private int nCityIdx2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fly_together_search);
		
		init();
	}
	
	private void init(){
		flyDateText = (TextView)findViewById(R.id.fly_date_text);
		flyFromEdit = (TextView)findViewById(R.id.fly_from_edit);
		flyToEdit = (TextView)findViewById(R.id.fly_to_edit);
		flySearchEdit = (EditText)findViewById(R.id.fly_search_edit);
		
		nowTime = new Time("GMT+8");
		nowTime.setToNow();
		_year = nowTime.year;
		_month = nowTime.month;
		_day = nowTime.monthDay;
		
		flyFromEdit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showChooseCountryDialog1(getString(R.string.create_fly_together_item_1));
			}
		});
		flyToEdit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showChooseCountryDialog2(getString(R.string.create_fly_together_item_2));
			}
		});
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		findViewById(R.id.btn_search).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				if(!TextUtils.isEmpty(flyFromEdit.getText().toString())){
					intent.putExtra("flyFrom", flyFromEdit.getText().toString());
				}
				if(!TextUtils.isEmpty(flyToEdit.getText().toString())){
					intent.putExtra("flyTo", flyToEdit.getText().toString());
				}
				if(!TextUtils.isEmpty(flySearchEdit.getText().toString().trim())){
					intent.putExtra("fTitle", flySearchEdit.getText().toString().trim());
				}
				if(_year != nowTime.year || _month != nowTime.month || _day != nowTime.monthDay){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					try {
						Date date = sdf.parse(_year+"-"+(_month+1)+"-"+_day);
						intent.putExtra("startTime", ""+date.getTime()/1000);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				setResult(111,intent);
				finish();
			}
		});
		
		findViewById(R.id.fly_date_layout).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showChooseDateDialog();
			}
		});
	}
	
	private void showChooseDateDialog(){
		new DatePickerDialog(this, new OnDateSetListener() {
            public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
                _year = year;
                _month = monthOfYear;
                _day = dayOfMonth;
                flyDateText.setText(_year+"年"+(_month+1)+"月"+_day+"日");
            }

        }, _year, _month, _day).show();
	}
	
	private void showChooseCountryDialog1(String title){
		View inputView = getLayoutInflater().inflate(R.layout.dialog_wheel_3_view, null);
		final WheelView wheel1 = (WheelView)inputView.findViewById(R.id.wheel_1);
		final WheelView wheel2 = (WheelView)inputView.findViewById(R.id.wheel_2);
		final WheelView wheel3 = (WheelView)inputView.findViewById(R.id.wheel_3);
		
		List<String> countryList = new ArrayList<String>();
		for(CountryData cd:BaseActivity.getCountryList(getApplicationContext())){
			countryList.add(cd.getN());
		}
        wheel1.setAdapter(new StringWheelAdapter(countryList));
        wheel1.setCurrentItem(nCountryIdx);
        wheel1.setCyclic(true);
        wheel1.setInterpolator(new AnticipateOvershootInterpolator());
        wheel1.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				nCountryIdx = newValue;
				nProvinceIdx = 0;
				nCityIdx = 0;
				
				List<String> provinceList = new ArrayList<String>();
				for(ProvinceData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP()){
					provinceList.add(cd.getN());
				}
		        wheel2.setAdapter(new StringWheelAdapter(provinceList));
		        wheel2.setCurrentItem(nProvinceIdx);
				
				List<String> cityList = new ArrayList<String>();
				for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
					cityList.add(cd.getN());
				}
				wheel3.setAdapter(new StringWheelAdapter(cityList));
				wheel3.setCurrentItem(nCityIdx);
			}});
		
		List<String> provinceList = new ArrayList<String>();
		for(ProvinceData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP()){
			provinceList.add(cd.getN());
		}
        wheel2.setAdapter(new StringWheelAdapter(provinceList));
        wheel2.setCurrentItem(nProvinceIdx);
        wheel2.setCyclic(true);
        wheel2.setInterpolator(new AnticipateOvershootInterpolator());
        wheel2.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				nProvinceIdx = newValue;
				nCityIdx = 0;
				
				List<String> cityList = new ArrayList<String>();
				for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
					cityList.add(cd.getN());
				}
				wheel3.setAdapter(new StringWheelAdapter(cityList));
				wheel3.setCurrentItem(nCityIdx);
			}});
        
		List<String> cityList = new ArrayList<String>();
		for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
			cityList.add(cd.getN());
		}
        wheel3.setAdapter(new StringWheelAdapter(cityList));
        wheel3.setCurrentItem(nCityIdx);
        wheel3.setCyclic(true);
        wheel3.setInterpolator(new AnticipateOvershootInterpolator());
        
		AlertDialog.Builder builder = new AlertDialog.Builder(FlyTogetherSearchActivity.this);
		builder.setTitle(title);
		builder.setView(inputView);  
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){
				nCountryIdx = wheel1.getCurrentItem();
				nProvinceIdx = wheel2.getCurrentItem();
				nCityIdx = wheel3.getCurrentItem();
				if(TextUtils.isEmpty(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getN())){
					flyFromEdit.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC().get(nCityIdx).getN());
				}else{
					flyFromEdit.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC().get(nCityIdx).getN());
				}
			}
		});  
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){}
		}); 
		builder.show();  
	}
	
	private void showChooseCountryDialog2(String title){
		View inputView = getLayoutInflater().inflate(R.layout.dialog_wheel_3_view, null);
		final WheelView wheel1 = (WheelView)inputView.findViewById(R.id.wheel_1);
		final WheelView wheel2 = (WheelView)inputView.findViewById(R.id.wheel_2);
		final WheelView wheel3 = (WheelView)inputView.findViewById(R.id.wheel_3);
		
		List<String> countryList = new ArrayList<String>();
		for(CountryData cd:BaseActivity.getCountryList(getApplicationContext())){
			countryList.add(cd.getN());
		}
        wheel1.setAdapter(new StringWheelAdapter(countryList));
        wheel1.setCurrentItem(nCountryIdx2);
        wheel1.setCyclic(true);
        wheel1.setInterpolator(new AnticipateOvershootInterpolator());
        wheel1.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				nCountryIdx2 = newValue;
				nProvinceIdx2 = 0;
				nCityIdx2 = 0;
				
				List<String> provinceList = new ArrayList<String>();
				for(ProvinceData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getP()){
					provinceList.add(cd.getN());
				}
		        wheel2.setAdapter(new StringWheelAdapter(provinceList));
		        wheel2.setCurrentItem(nProvinceIdx2);
				
				List<String> cityList = new ArrayList<String>();
				for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getP().get(nProvinceIdx2).getC()){
					cityList.add(cd.getN());
				}
				wheel3.setAdapter(new StringWheelAdapter(cityList));
				wheel3.setCurrentItem(nCityIdx2);
			}});
		
		List<String> provinceList = new ArrayList<String>();
		for(ProvinceData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getP()){
			provinceList.add(cd.getN());
		}
        wheel2.setAdapter(new StringWheelAdapter(provinceList));
        wheel2.setCurrentItem(nProvinceIdx2);
        wheel2.setCyclic(true);
        wheel2.setInterpolator(new AnticipateOvershootInterpolator());
        wheel2.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				nProvinceIdx2 = newValue;
				nCityIdx2 = 0;
				
				List<String> cityList = new ArrayList<String>();
				for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getP().get(nProvinceIdx2).getC()){
					cityList.add(cd.getN());
				}
				wheel3.setAdapter(new StringWheelAdapter(cityList));
				wheel3.setCurrentItem(nCityIdx2);
			}});
        
		List<String> cityList = new ArrayList<String>();
		for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getP().get(nProvinceIdx2).getC()){
			cityList.add(cd.getN());
		}
        wheel3.setAdapter(new StringWheelAdapter(cityList));
        wheel3.setCurrentItem(nCityIdx2);
        wheel3.setCyclic(true);
        wheel3.setInterpolator(new AnticipateOvershootInterpolator());
        
		AlertDialog.Builder builder = new AlertDialog.Builder(FlyTogetherSearchActivity.this);
		builder.setTitle(title);
		builder.setView(inputView);  
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){
				nCountryIdx2 = wheel1.getCurrentItem();
				nProvinceIdx2 = wheel2.getCurrentItem();
				nCityIdx2 = wheel3.getCurrentItem();
				if(TextUtils.isEmpty(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getP().get(nProvinceIdx2).getN())){
					flyToEdit.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getP().get(nProvinceIdx2).getC().get(nCityIdx2).getN());
				}else{
					flyToEdit.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getP().get(nProvinceIdx2).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx2).getP().get(nProvinceIdx2).getC().get(nCityIdx2).getN());
				}
			}
		});  
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){}
		}); 
		builder.show();  
	}

}
